package cn.school.controller.student;

import cn.school.common.api.CommonPage;
import cn.school.common.api.CommonResult;
import cn.school.common.utils.SysUserUtils;
import cn.school.student.entity.TExamPerformance;
import cn.school.student.service.TExamPerformanceService;
import cn.school.sys.entity.SysUser;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 考試成績表(TExamPerformance)表控制层
 *
 * @author makejava
 * @since 2023-02-16 14:41:12
 */
@RestController
@RequestMapping("tExamPerformance")
public class TExamPerformanceController {
    /**
     * 服务对象
     */
    @Autowired
    private TExamPerformanceService tExamPerformanceService;

    /**
     * 分页查询
     *
     * @param tExamPerformance 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @GetMapping("/query")
    @ApiOperation("query=>试卷分页查询")
    public CommonResult<CommonPage<TExamPerformance>> queryByPage(TExamPerformance tExamPerformance, Integer pageNum, Integer pageSize) {
        IPage<TExamPerformance> tExamPerformanceIPage = tExamPerformanceService.queryByPage(tExamPerformance, PageRequest.of((pageNum-1), pageSize));
        return CommonResult.success(CommonPage.restPage(tExamPerformanceIPage));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @ApiOperation("queryById=> 试卷详细信息")
    public  CommonResult<TExamPerformance> queryById(@PathVariable("id") Long id) {
        return CommonResult.success(this.tExamPerformanceService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param tExamPerformance 实体
     * @return 新增结果
     */
    @PostMapping("/insert")
    @ApiOperation("insert=>试卷新增结果")
    public CommonResult<TExamPerformance> add(@RequestBody TExamPerformance tExamPerformance) {
        SysUser sysUser = SysUserUtils.getSysUser();

        tExamPerformance.setCreateBy(sysUser.getUsername());
        tExamPerformance.setCreateTime(new Date());

        Integer insert = this.tExamPerformanceService.insert(tExamPerformance);
        if (insert!=0){
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    /**
     * 编辑数据
     *
     * @param tExamPerformance 实体
     * @return 编辑结果
     */
    @PostMapping("/update")
    @ApiOperation("update=>更新试卷信息")
    public CommonResult<TExamPerformance> edit(@RequestBody TExamPerformance tExamPerformance) {
        SysUser sysUser = SysUserUtils.getSysUser();

        tExamPerformance.setUpdateBy(sysUser.getUsername());
        tExamPerformance.setUpdateTime(new Date());


        Integer update = this.tExamPerformanceService.update(tExamPerformance);
        if (update!=0){
            return CommonResult.success();
        }


        return  CommonResult.failed();
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("/delete")
    @ApiOperation("delete=>删除试卷信息")
    public CommonResult<TExamPerformance> deleteById(Long id) {
        boolean b = this.tExamPerformanceService.deleteById(id);
        if (b){
            return CommonResult.failed();
        }
        return CommonResult.success();
    }

}

